

package com.hazelcast.jet.core.metrics;

import com.hazelcast.internal.serialization.DataSerializerHook;
import com.hazelcast.internal.serialization.impl.FactoryIdHelper;
import com.hazelcast.nio.serialization.DataSerializableFactory;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.annotation.PrivateApi;

import static com.hazelcast.jet.impl.JetFactoryIdHelper.JET_JOB_METRICS_DS_FACTORY;
import static com.hazelcast.jet.impl.JetFactoryIdHelper.JET_JOB_METRICS_DS_FACTORY_ID;

/**
 * A Java Service Provider hook for Hazelcast's Identified Data Serializable
 * mechanism. This is private API.
 */
@PrivateApi
public class MetricsDataSerializerHook implements DataSerializerHook {

    /**
     * Serialization ID of the {@link JobMetrics} class.
     */
    public static final int JOB_METRICS = 0;

    /**
     * Serialization ID of the {@link Measurement} class.
     */
    public static final int MEASUREMENT = 1;

    static final int FACTORY_ID = FactoryIdHelper.getFactoryId(JET_JOB_METRICS_DS_FACTORY, JET_JOB_METRICS_DS_FACTORY_ID);

    @Override
    public int getFactoryId() {
        return FACTORY_ID;
    }

    @Override
    public DataSerializableFactory createFactory() {
        return new Factory();
    }

    private static class Factory implements DataSerializableFactory {
        @Override
        public IdentifiedDataSerializable create(int typeId) {
            return switch (typeId) {
                case JOB_METRICS -> new JobMetrics();
                case MEASUREMENT -> new Measurement();
                default -> throw new IllegalArgumentException("Unknown type id " + typeId);
            };
        }
    }
}
